#include<iostream>
using namespace std;
int main(){
	int b,p,k;
	cin>>b>>p>>k;
	int h;
	int x=(b%k) * (p-1%k) %k;
	if(x<b){
		cout<<x+1;
	}
	else{
		h=x-b; 
	    cout<<h-2;
	} 
}
